package com.ssm.controller;

import com.alibaba.excel.EasyExcel;
import com.ssm.pojo.Manager;
import com.ssm.pojo.Msg;
import com.ssm.pojo.Stu;
import com.ssm.pojo.StuMistake;
import com.ssm.service.ManagerService;
import com.ssm.service.StuMistakeService;
import com.ssm.service.StuService;
import com.ssm.util.ManagerDataListener;
import com.ssm.util.StuDataListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Controller
public class UpAndLoad {

    @Autowired
    StuService stuService;

    @Autowired
    ManagerService managerService;

    @Autowired
    StuMistakeService stuMistakeService;

    @ResponseBody
    @RequestMapping("/findAll")
    public Msg findAll(String cdorm){
        List<Stu> cdorm1 = stuService.find_cdorm(cdorm);
        if(cdorm1.isEmpty()){
            return Msg.fail().add("msg","宿舍楼不存在");
        }
        return Msg.success();
    }

    @RequestMapping("/toUpLoad")
    public String toUpLoad() {
        return "smanager/uploaddatas";
    }
    @RequestMapping("/toUpload")
    public String toUpload() {
        return "manager/uploaddata";
    }


    @RequestMapping("/goUp")
    public String importExcel(MultipartFile file, int info, @RequestParam(defaultValue = "1") int info_stu) throws Exception {
        if (info == 1) {
            EasyExcel.read(file.getInputStream(), Manager.class, new ManagerDataListener(managerService)).sheet().doRead();
        } else if (info == 2) {
            EasyExcel.read(file.getInputStream(), Stu.class, new StuDataListener(stuService)).sheet().doRead();
            if (info_stu == 2) {
                return "manager/uploaddata";
            }
        }
        return "smanager/uploaddatas";
    }

    //超级管理员
    @RequestMapping("/goLoadManager")
    public String goLoad(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("管理员信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Manager.class).sheet("模板").doWrite(managerService.list());
        return "smanager/uploaddatas";
    }

    @ResponseBody
    @RequestMapping("/goLoadStu")
    public Msg goloadStu(HttpServletResponse response, String cdorm, HttpSession session) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String filname="";
        try {
            if(cdorm==null || cdorm==""){
                filname="全部";
                String fileName = URLEncoder.encode(filname + "学生信息", "UTF-8");
                response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
                EasyExcel.write(response.getOutputStream(), Stu.class).sheet("模板").doWrite(stuService.find_cdorm(cdorm));
                return Msg.success();
            }else {
                List<Stu> cdorm1 = stuService.find_cdorm(cdorm);
                if(!cdorm1.isEmpty()){
                    String fileName = URLEncoder.encode(cdorm + "学生信息", "UTF-8");
                    response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
                    EasyExcel.write(response.getOutputStream(), Stu.class).sheet("模板").doWrite(stuService.find_cdorm(cdorm));
                    return Msg.success();
                } else{
                    session.setAttribute("msg","宿舍不存在");
                    return Msg.fail().add("msg","宿舍不存在");
                }
            }
        } catch (Exception e) {
            session.setAttribute("msg","操作错误");
            return Msg.fail().add("msg","操作错误");
        }
    }

    @RequestMapping("/goLoadStuMistake")
    public String goLoadStu(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("学生缺勤信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), StuMistake.class).sheet("模板").doWrite(stuMistakeService.listAll(null));
        return "smanager/uploaddatas";
    }
}